﻿#region Apache License
/* 
   Copyright 2011 Jorge Teixeira

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
#endregion

using System;

namespace AdHoc.Wavesque
{
    /// <summary>
    /// Object representation of a sawtooth wave.
    /// </summary>
    public class SawToothesque : Esque
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SawToothesque"/> class.
        /// </summary>
        protected SawToothesque()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SawToothesque"/> class.
        /// </summary>
        /// <param name="step">Value for position increases.</param>
        /// <param name="amplitude">Amplitude of the oscillation.</param>
        /// <param name="frequency">Frequency of the oscillation (in Hz).</param>
        /// <param name="phase">Phase of the oscillation (in radians).</param>
        public SawToothesque(double step, double amplitude, double frequency, double phase)
        {
            Step = step;
            Amplitude = amplitude;
            Frequency = frequency;
            Phase = phase;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SawToothesque"/> class.
        /// </summary>
        /// <param name="amplitude">Amplitude of the oscillation.</param>
        /// <param name="frequency">Frequency of the oscillation (in Hz).</param>
        /// <param name="phase">Phase of the oscillation (in radians).</param>
        public SawToothesque(double amplitude, double frequency, double phase)
        {
            if (frequency != 0.0)
            {
                Step = EsqueHelpers.GetStepDefaultValue(frequency);
            }
            Amplitude = amplitude;
            Frequency = frequency;
            Phase = phase;
        }

        /// <summary>
        /// Creates a synchronized version of the given <see cref="SawToothesque"/> instance.
        /// </summary>
        /// <param name="source">The instance.</param>
        /// <returns>Synchronized version of the given instance.</returns>
        public static SawToothesque Synchronized(SawToothesque source)
        {
            if (!source.IsSynchronized)
            {
                return new SynchronizedSawToothesque(source);
            }
            return source;
        }

        /// <summary>
        /// Calculates a given number of points of a waveform for a given initial position.
        /// </summary>
        /// <param name="numberPoints">Number of points to compute.</param>
        /// <param name="x">Abscissae.</param>
        /// <param name="y">Ordinates.</param>
        /// <param name="x0">Initial position.</param>
        public override void GetValues(uint numberPoints, out double[] x, out double[] y, double x0)
        {
            double cX = x0;
            double cY = 0.0;

            if (numberPoints > 0)
            {
                x = new double[numberPoints];
                y = new double[numberPoints];
            }
            else
            {
                x = new double[1];
                y = new double[1];

                x[0] = x0;
                y[0] = 0.0;
            }

            if (Frequency <= 0.0)
                return;

            double period = 1.0 / Frequency;
            double w = 2 * Math.PI * Frequency;
            double shift = Phase / w;
            for (int i = 0; i < numberPoints; i++)
            {
                if (i != 0)
                {
                    cX = cX + Step;
                }
    
                cY = Amplitude * ((cX / period) - Math.Floor(cX / period));

                x[i] = cX + shift;
                y[i] = cY;
            }
        }
    }
}
